import { recordTypeList } from '../../../utils/jk/status-option-list';

export default {
	data() {
		return {
			recordTypeList: recordTypeList,
			exportLoading: false
		};
	},
	methods: {
		/** 导出按钮操作 */
		onReportBtn() {
			let headers = [];
			/**
				@params title { String } 名称
			    @params field { Boolean } 字段
				@params isA { Boolean } 是否是完整的字段，true时需要根据行数据上的unit在后缀动态拼接单位，如：endOutputM、endOutputKg
			 * * */
			headers = [
				{ title: '机台', isA: false, field: 'machineName' },
				{ title: '序号', isA: false, field: 'sort' },
				{ title: '开始时间', isA: false, field: 'startDateTime' },
				{ title: '结束时间', isA: false, field: 'endDateTime' },
				{ title: '分段类型', isA: false, field: 'recordType' },
				{ title: '采集品种', isA: false, field: 'productCode' },
				{ title: '开始产量表数', isA: true, field: 'startOutput' },
				{ title: '结束产量表数', isA: true, field: 'endOutput' },
				{ title: '产量', isA: true, field: 'output' },
				{ title: '单位', isA: false, field: 'unit' },
				{ title: '运行时长(分钟)', isA: false, field: 'runningTime' },
				{ title: '停机时长(分钟)', isA: false, field: 'shutdownTime' }
			];
			const titles = headers.map(k => k.title);
			const keyNames = headers.map(k => k.field);
			this.$modal.confirm('是否确认导出所有数据数据项?').then(() => {
				this.exportLoading = true;
			}).then(() => {
				this.exportLoading = false;
				return this.apiItem['page'](this.searchParams).then(res => {
					if (res.data.status === 200) {
						import('@/vendor/Export2Excel').then(excel => {
							const list = [];
							const resData = res.data.res || [];
							resData.forEach(x => {
								if (x.machineProductRecordList) {
									x.machineProductRecordList.forEach((timeItem, timeIndex) => {
										list.push({
											machineName: x.machineName,
											unit: x.unit,
											sort: timeIndex + 1,
											...timeItem,
											recordType: this.recordTypeList.find(x => x.id === timeItem.recordType) ? this.recordTypeList.find(x => x.id === timeItem.recordType).name : ''
										});
									});
								}
							});
							const data = list.map(rowItem => headers.map(headerItem => {
								return headerItem.isA ? rowItem.unit === 'kg' ? rowItem[`${headerItem.field}Kg`] : rowItem[`${headerItem.field}M`] : rowItem[headerItem.field];
							}));
							const processItem = this.processList.find(x => x.code === this.searchParams.processCode) || {};
							const shiftItem = this.shiftList.find(x => x.id === this.searchParams.shiftId) || {};
							excel.export_json_to_excel({
								header: titles,
								data,
								filename: `${this.$route.meta.title}-${processItem.name || '全工序'}-${this.searchParams.belongDate}-${shiftItem.name}`,
								autoWidth: true,
								bookType: 'xlsx'
							});
						});
					}
				});
			}).catch(() => {});
		}
	}
};
